package com.autocar.reader.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.autocar.reader.R;
import com.autocar.reader.bean.NewsBean;
import com.autocar.reader.net.ImageLoader;
import com.autocar.reader.net.ImageLoader.ImageCallback;
import com.autocar.reader.util.ConstantData;
import com.autocar.reader.util.ImageUtils;
import com.autocar.reader.view.XListView;
import com.org.any.autocar.fragment.IopenArticle;

public class InformationAdapter extends BaseAdapter implements ImageCallback {

	private LayoutInflater inflater;
	private List<NewsBean> listnews;
	private XListView rootView;
	private ImageLoader load;
	private IopenArticle iopen;
	private Bitmap bmp;

	public InformationAdapter(Context context, XListView rootView,
			IopenArticle iopen) {
		inflater = LayoutInflater.from(context);
		this.rootView = rootView;
		// load = new ImageLoader(this);
		this.iopen = iopen;

		bmp = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.no_img_0);
	}

	public void setListNews(List<NewsBean> listnews) {
		this.listnews = listnews;
		notifyDataSetChanged();
	}

	public List<NewsBean> getLNewsBeans() {
		return listnews;
	}

	@Override
	public int getCount() {
		return listnews == null ? 0 : listnews.size();
	}

	@Override
	public NewsBean getItem(int position) {
		return listnews.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler holder;
		if (convertView == null) {
			holder = new ViewHodler();
			convertView = inflater.inflate(
					R.layout.information_list_item_layout, null);
			holder.broseTv = (TextView) convertView
					.findViewById(R.id.information_list_tv_browse_num);
			holder.describeTv = (TextView) convertView
					.findViewById(R.id.information_list_tv_describe);
			holder.praiseIv = (ImageView) convertView
					.findViewById(R.id.information_list_iv_praise);
			holder.praiseTv = (TextView) convertView
					.findViewById(R.id.information_list_tv_praise);
			holder.titleTv = (TextView) convertView
					.findViewById(R.id.information_list_tv_title);
			holder.timeTv = (TextView) convertView
					.findViewById(R.id.information_list_tv_time);
			holder.picIv = (ImageView) convertView
					.findViewById(R.id.information_list_iv_pic);
			convertView.setTag(holder);
		} else {
			holder = (ViewHodler) convertView.getTag();
		}

		final NewsBean bean = getItem(position);
		holder.broseTv.setText(bean.getClicks());
		holder.describeTv.setText(bean.getSummary());
		holder.praiseTv.setText(bean.getPraises());
		holder.titleTv.setText(bean.getTitle());
		holder.timeTv.setText(bean.getPublished());
//		final String url = bean.getS_pic();
//		holder.picIv.setTag(url);
//		String filePath = ConstantData.getImagePath(url);
//		Bitmap bitmap = ConstantData.getBitmapBypath(filePath);
//		holder.picIv.setImageBitmap(bitmap);
		Log.e("mg", "url:" + bean.getS_pic());
//		if (bitmap == null) {
//			holder.picIv.setImageResource(R.drawable.no_img_0);
//			load.loadDrawable(url, filePath);
//		}
		ImageUtils.fb.display(holder.picIv, bean.getS_pic(),bmp);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (iopen != null) {
					iopen.opentArticle(bean);
				}
			}
		});
		return convertView;
	}

	class ViewHodler {
		TextView titleTv;
		TextView describeTv;
		ImageView praiseIv;
		TextView praiseTv;
		TextView broseTv;
		TextView timeTv;
		ImageView picIv;
	}

	@Override
	public void imageLoaded(String filePath, String imageUrl) {
		ImageView view = (ImageView) rootView.findViewWithTag(imageUrl);
		if (view != null) {
			view.setImageBitmap(ConstantData.getBitmapBypath(filePath));
		}
	}

}
